<?php

namespace App\Admin\Metrics\Examples;

use App\Models\Feedback;
use App\Models\User;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Metrics\Bar;
use Illuminate\Http\Request;

class Sessions extends Bar
{
    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();

        $color = Admin::color();

        $dark35 = $color->dark35();

        // 卡片内容宽度
        $this->contentWidth(5, 7);
        // 标题
        $this->title('问题反馈');
        // 设置下拉选项
        $this->dropdown([
            '1' => '天数据',
            '7' => '周数据',
            '30' => '月数据',
            '365' => '年数据',
        ]);
        // 设置图表颜色
        $this->chartColors([
            $dark35,
            $dark35,
            $color->primary(),
            $dark35,
            $dark35,
            $dark35
        ]);
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {

        $values = $this->getValue();
        // 图表数据
        $this->withChart([
            [
                'name' => '问题反馈',
                'data' =>$values,
            ],
        ]);

        switch ($request->get('option')) {
            case '1':
                $start_time = date("Y-m-d 00:00:00");
                $end_time = date('Y-m-d 23:59:59');
                $userCount = Feedback::whereDate('created_at','>=',$start_time)->whereDate('created_at','<=',$end_time)->count();
                $this->withContent($userCount);
                break;
            case '7':
                $start_time = date("Y-m-d",strtotime("-7 day"));
                $end_time = date('Y-m-d');
                $userCount = Feedback::whereDate('created_at','>=',$start_time)->whereDate('created_at','<=',$end_time)->count();
                $this->withContent($userCount);
            case '30':
                $start_time = date("Y-m-01 00:00:00");
                $end_time = date('Y-m-31 23:59:59');
                $userCount = Feedback::whereDate('created_at','>=',$start_time)->whereDate('created_at','<=',$end_time)->count();
                $this->withContent($userCount);
                break;
            case '365':
                $start_time = date("Y-01-01 00:00:00");
                $end_time = date('Y-12-31 23:59:59');
                $userCount = Feedback::whereDate('created_at','>=',$start_time)->whereDate('created_at','<=',$end_time)->count();
                $this->withContent($userCount);
                break;
            default:
                // 卡片内容
//                $this->withContent('2.7k', '+5.2%');
//
                $start_time = date("Y-m-d 00:00:00");
                $end_time = date('Y-m-d 23:59:59');
                $userCount = Feedback::whereDate('created_at','>=',$start_time)->whereDate('created_at','<=',$end_time)->count();
                $this->withContent($userCount);
        }
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => $data,
        ]);
    }

    /**
     * 设置卡片内容.
     *
     * @param string $title
     * @param string $value
     * @param string $style
     *
     * @return $this
     */
    public function withContent($value, $style = 'success')
    {
        $minHeight = '183px';
        return $this->content(
            <<<HTML
<div class="d-flex p-1 flex-column justify-content-between" style="padding-top: 0;width: 100%;height: 100%;min-height: {$minHeight}">
    <div class="text-left">
        <h1 class="font-lg-2 mt-2 mb-0" style="font-size: 20px;">{$value}</h1>
        <h5 class="font-medium-2" style="margin-top: 10px;">
            <span class="text-{$style}">{$this->title} </span>
        </h5>
    </div>

    <a href="/admin/feedback" class="btn btn-primary shadow waves-effect waves-light">View Details <i class="feather icon-chevrons-right"></i></a>
</div>
HTML
        );
    }


    /**
     * 获取图表数据
     */
    public function getValue()
    {
        //当天数据
        $start_time_day = date("Y-m-d 00:00:00");
        $end_time_day = date('Y-m-d 23:59:59');
        //用户数量
        $day = Feedback::whereDate('created_at','>=',$start_time_day)->whereDate('created_at','<=',$end_time_day)->count();

        //七天数据
        $start_time_seven = date("Y-m-d",strtotime("-7 day"));
        $end_time_seven = date('Y-m-d');
        $seven = Feedback::whereDate('created_at','>=',$start_time_seven)->whereDate('created_at','<=',$end_time_seven)->count();

        //月数据
        $start_time_month = date("Y-m-01 00:00:00");
        $end_time_month = date('Y-m-31 23:59:59');
        $month = Feedback::whereDate('created_at','>=',$start_time_month)->whereDate('created_at','<=',$end_time_month)->count();

        //年数据
        $start_time_year = date("Y-01-01 00:00:00");
        $end_time_year = date('Y-12-31 23:59:59');
        $year = Feedback::whereDate('created_at','>=',$start_time_year)->whereDate('created_at','<=',$end_time_year)->count();

        //总数据
        $all =  Feedback::count();

        $data = [
            $day,$seven,$month,$year,$all
        ];

        return $data;

    }

}
